package com.meta.platform.company.openapi;

import com.meta.platform.company.dto.QueryCompanyDto;
import com.meta.platform.company.util.HttpUtil;
import com.meta.platform.company.util.MD5Util;
import java.util.HashMap;
import java.util.Map;

public class QccApi {

//    private static final String QCC_KEY = "cd3352f7b88640129531303910cbaa52";
//    private static final String QCC_SEC = "6CE8812C03A37EF9E0ECC1558C9ED4C2";

//    private static final String QCC_KEY = "6e88b7ff19c045bd93540ff341497a2e";
//    private static final String QCC_SEC = "BA78AD09CB53C21E1322AEBB5654C67B";

    private static final String QCC_KEY = "8b420b32286044d4b971cbc07736311e";
    private static final String QCC_SEC = "27B93AC94161B4265417A612994DCF69";

    private static final String LIST_URL = "http://api.qichacha.com/ECIV4/Search";
    private static final String BASIC_URL = "http://api.qichacha.com/ECIV4/GetBasicDetailsByName";
    private static final String SHAREHOLDER_URL = "http://api.qichacha.com/ECIPartner/GetList";
    private static final String MEMBER_URL = "http://api.qichacha.com/ECIEmployee/GetList";
    private static final String CHANGE_URL = "http://api.qichacha.com/ECIChange/GetList";
    private static final String BRANCH_URL = "http://api.qichacha.com/ECIBranch/GetList";
    // TODO：先不加
    private static final String OPERATION_URL = "http://api.qichacha.com/ECIV4/Search";

    private static String sendRequest(String url) {
        System.out.println(url);
        Map<String, String> headers = new HashMap<>();
        Long currentTimespan = System.currentTimeMillis() / 1000L;
        String token = createKey(currentTimespan);
        System.out.println(token);
        headers.put("Token", token);
        headers.put("Timespan", currentTimespan.toString());
        return HttpUtil.sendGet(url, headers);
    }

    public static String queryCompanyList(QueryCompanyDto dto){

        String url = LIST_URL;
        url += ("?key=" + QCC_KEY + "&");

        url += dto.toString();

        String res = sendRequest(url);

        return res;
    }

    public static String queryBasicInfo(QueryCompanyDto dto){

        String url = BASIC_URL;
        url += ("?key=" + QCC_KEY + "&");
        url += dto.toString();
        String res = sendRequest(url);
        return res;
    }

    public static String queryShareholderInfo(QueryCompanyDto dto){

        String url = SHAREHOLDER_URL;
        url += ("?key=" + QCC_KEY + "&");
        url += dto.toString();
        String res = sendRequest(url);
        return res;
    }
    public static String queryMemberInfo(QueryCompanyDto dto){
        String url = MEMBER_URL;
        url += ("?key=" + QCC_KEY + "&");
        url += dto.toString();
        String res = sendRequest(url);
        return res;
    }

    public static String queryChangeInfo(QueryCompanyDto dto){
        String url = CHANGE_URL;
        url += ("?key=" + QCC_KEY + "&");
        url += dto.toString();
        String res = sendRequest(url);
        return res;
    }

    public static String queryBranchInfo(QueryCompanyDto dto){
        String url = BRANCH_URL;
        url += ("?key=" + QCC_KEY + "&");
        url += dto.toString();
        String res = sendRequest(url);
        return res;
    }

//    public String queryOperationInfo(QueryCompanyDto dto){
//        String url = OPERATION_URL;
//        url += ("?key=" + QCC_KEY + "&");
//        url += dto.toString();
//        String res = sendRequest(url);
//        return res;
//    }

    private static String createKey(Long timestamp){

        String res = null;

        StringBuilder builder = new StringBuilder();
        builder.append(QCC_KEY)
                .append(timestamp)
                .append(QCC_SEC);

        String comStr = builder.toString();
        try {
           res = MD5Util.encodeMD5Hex(comStr).toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    public static void main(String[] args) {

//        QueryCompanyDto dto = new QueryCompanyDto();
//        dto.setKeyword("艾佳生活");
//        dto.setPageSize(20);
//        dto.setPageIndex(1);
//        System.out.println(dto);
//
//        System.out.println(queryCompanyList(dto));

//        QueryCompanyMemberDto memberDto = new QueryCompanyMemberDto();
//        memberDto.setSearchKey("91320115302368943U");
//        System.out.println(queryCompanyMember(memberDto));

    }

}
